﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using RM.WebClient.RMServices;

namespace RM.WebClient
{
    public partial class CandidateList : UserControl
    {
        public CandidateList()
        {            
            InitializeComponent();            
        }

        void candidateServices_GetAllCompleted(object sender, GetAllCompletedEventArgs e)
        {
            try
            {
                this.dataGrid.DataContext = e.Result;
                lblWait.Visibility = Visibility.Collapsed;
                progressBar.Visibility = Visibility.Collapsed;
            }
            catch (Exception)
            {
            }
        }

        private void Link_CandidateDetail_Click(object sender, System.Windows.RoutedEventArgs e)
        {            
            gridCandidateList.Children.Clear();
            gridCandidateList.Children.Add(new CandidateDetail());            
        }

        private void autoCompleteNameSearch_TextChanged(object sender, RoutedEventArgs e)
        {
            CandidateServiceClient candidateServices = new CandidateServiceClient();
            String sTextContents = autoCompleteNameSearch.Text;
            candidateServices.GetAllBySearchAsync(sTextContents);
            candidateServices.GetAllBySearchCompleted += new EventHandler<GetAllBySearchCompletedEventArgs>(candidateServices_GetAllBySearchCompleted);
        }

        void candidateServices_GetAllBySearchCompleted(object sender, GetAllBySearchCompletedEventArgs e)
        {
            dataGrid.ItemsSource = e.Result;   
        }

        private void reArrangeDataGridColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
        {
            switch (e.Column.Header.ToString())
            { 
                case "FirstName":
                case "First Name":
                    e.Column.DisplayIndex = 0;
                    break;

                case "LastName":
                case "Last Name":
                    e.Column.DisplayIndex = 1;
                    break;

                case "DoB":
                    e.Column.DisplayIndex = 2;
                    break;

                case "Sex":
                    e.Column.DisplayIndex = 3;
                    break;

                case "EmailAddress":
                case "Email Address":
                    e.Column.DisplayIndex = 4;
                    break;

                case "CurrentLocation":
                case "Current Location":
                    e.Column.DisplayIndex = 5;
                    break;

                case "Country":
                    e.Column.DisplayIndex = 6;
                    break;

                case "MobileNo":
                case "Mobile No":
                    e.Column.DisplayIndex = 7;
                    break;

                //case "AppliedForPost":
                //    e.Column.DisplayIndex = 12;
                //    break;

                //case "AreaCode":
                //    e.Column.DisplayIndex = 13;
                //    break;

                //case "CountryCode":
                //    e.Column.DisplayIndex = 14;
                //    break;
                
                default:
                    break;
            }
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                CandidateServiceClient candidateServices = new CandidateServiceClient();
                candidateServices.GetAllAsync(candidateServices);
                candidateServices.GetAllCompleted += new EventHandler<GetAllCompletedEventArgs>(candidateServices_GetAllCompleted);
            }
            catch (Exception)
            {
            }
        }

        private void imgAddUser_MouseLeftButtonUp(object sender, MouseEventArgs e)
        {
            gridCandidateList.Children.Clear();
            gridCandidateList.Children.Add(new CandidateDetail());
        }       
    }
}
